//
//  XIObject.m
//  XI Engine
//
//  Created by Balazs Faludi on 30.05.09.
//  Copyright 2009 Balazs Faludi. All rights reserved.
//

#import "XIObject.h"
#import "XIEngine.h"

@implementation XIObject

@synthesize layer;
@synthesize hidden;
@synthesize tag;

#pragma mark -
#pragma mark Initializer

- (id)init
{
	if ((self = [super init])) {
		
	}
	return self;
}

- (void)loadFromDictionary:(NSDictionary *)objectDict
{
	@try {
		// If the object has some properties declared in the file, set them. Or send them as a message.
		NSDictionary *objectProperties = [objectDict objectForKey:@"properties"];
		if(objectProperties)
		{
			NSEnumerator *objectPropertyEnumerator = [objectProperties keyEnumerator];
			NSString *objectPropertyName;
			while ((objectPropertyName = [objectPropertyEnumerator nextObject])) {
				if(!setValueOfObjectForPropertyName(&self, objectPropertyName, [objectProperties valueForKey:objectPropertyName]))
				{performSelectorOfObject(&self, objectPropertyName, [objectProperties valueForKey:objectPropertyName]);}
			}
		}
	}
	@catch (NSException * e) {
		[NSException raise:@"XILoadingException" format:@"Layer cannot be loaded from specified dictionary."];
		return;
	}
}

- (id)initFromDictionary:(NSDictionary *)objectDict
{
	if ((self = [self init])) {
		[self loadFromDictionary:objectDict];
	}
	return self;
}

- (NSMutableDictionary *)saveStateToDictionary
{
	return [NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:hidden], @"hidden",
															 [NSNumber numberWithInteger:tag], @"tag", nil];
}

- (NSDictionary *)saveToDictionary
{
	NSMutableDictionary *mutable = [NSMutableDictionary dictionaryWithCapacity:2];
	[mutable setObject:[self className] forKey:@"class"];
	[mutable setObject:[self saveStateToDictionary] forKey:@"properties"];
	return mutable;	
}

#pragma mark -
#pragma mark Abstract

- (void)willBeAddedToLayer:(XILayer *)newLayer{}
- (void)wasAddedToLayer:(XILayer *)newLayer{}
//- (BOOL)containsPoint:(CGPoint)point{}

#pragma mark -
#pragma mark Loop

#if TARGET_OS_IPHONE
- (void)update
{
	
}
#endif

- (void)draw
{
	
}

#if TARGET_OS_MAC
- (void)drawForSceneBuilder
{
	[self draw];
}
#endif


@end
